#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fcntl.h>
#include <iostream>
#include <sys/stat.h>
#include <unistd.h>

using namespace std;

int main(int argc, char **argv) {
  int fd = open("/proc/sys/kernel/pid_max", (argc > 1) ? O_RDWR : O_RDONLY);
  if (fd == -1) {
    perror("open");
    exit(EXIT_FAILURE);
  }

  char line[80];
  int n = read(fd, line, sizeof(line));
  if (n == -1) {
    perror("read");
    exit(EXIT_FAILURE);
  }

  if (argc > 1) {
    cout << "Old value: ";
  }
  printf("%.*s", (int)n, line);

  if (argc > 1) {
    if (write(fd, argv[1], strlen(argv[1])) != strlen(argv[1])) {
      perror("write");
      exit(EXIT_FAILURE);
    }

    system("echo /proc/sys/kernel/pid_max now contains "
           "`cat /proc/sys/kernel/pid_max`");
  }

  exit(EXIT_SUCCESS);
}
